package com.mnnu.controller;

import com.mnnu.pojo.Address;
import com.mnnu.pojo.Result;
import com.mnnu.pojo.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.tags.Param;

import java.time.LocalDateTime;
import java.util.List;

@RestController
public class HelloController {

    @RequestMapping("/getParam")
    public String get(HttpServletRequest httpServletRequest) {
        String name = httpServletRequest.getParameter("name");
        String age = httpServletRequest.getParameter("age");

        System.out.println(name + age);
        return "OK";
    }

    @RequestMapping("/postParam")
    public String post(@RequestParam(name = "name") String username, Integer age) {
        System.out.println(username + age);
        return "OK";
    }

    @RequestMapping("/complexPojo")
    public String complexPojo(User user) {
        System.out.println(user);

        return "OK";
    }

    @RequestMapping("/listParam")
    public String listParam(@RequestParam List<String> hobby) {
        System.out.println(hobby);

        return "OK";
    }

    @RequestMapping("/dateParam")
    public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime updateTime) {
        System.out.println(updateTime);
        return "OK";
    }

    @RequestMapping("/jsonParam")
    public String jsonParam(@RequestBody User user) {
        System.out.println(user);
        return "OK";
    }

    @RequestMapping("/path/{id}")
    public String pathParam(@PathVariable Integer id) {
        System.out.println(id);
        return "OK";
    }

    @RequestMapping("/resultRtn")
    public Result resultRtn() {
        User user = new User("zhangsan", 18, new Address("beijing", "beijing"));
        return Result.success(user);
    }
}
